{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$$cost=\\dfrac{1}{N}\\sum ^{N}_{n=1}\\left( \\widehat{y}_{n}-y_{n}\\right) ^{2}$$\n",
    "\n",
    "三、[梯度下降](https://so.csdn.net/so/search?q=%E6%A2%AF%E5%BA%A6%E4%B8%8B%E9%99%8D&spm=1001.2101.3001.7020)\n",
    "------------------------------------------------------------------------------------------------------\n",
    "\n",
    "类似[牛顿迭代法](https://so.csdn.net/so/search?q=%E7%89%9B%E9%A1%BF%E8%BF%AD%E4%BB%A3%E6%B3%95&spm=1001.2101.3001.7020)/ 二分法，对cost func 求导 , 利用偏导进行迭代，使得cost func 达到最小值。\n",
    "\n",
    "![](https://img-blog.csdnimg.cn/img_convert/9b8fe9d396ca643d6b62693bf627323c.png)\n",
    "\n",
    "具体的求导过程\n",
    "\n",
    "![](https://img-blog.csdnimg.cn/img_convert/f16d0ff50dd6280d501a1cd928571a45.png)\n",
    "\n",
    "关键迭代式：\n",
    "\n",
    "![](https://img-blog.csdnimg.cn/img_convert/8821b49fbfe7e8853d1e1e82a73a521e.png)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 梯度下降法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "w =  2.0 epoch =  189\n",
      "After train forward(4) =  7.99999996372013\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYIAAAEJCAYAAACZjSCSAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuNCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8QVMy6AAAACXBIWXMAAAsTAAALEwEAmpwYAAAdRklEQVR4nO3df5RfdX3n8edrfv9MJskMJOQHSQAVofKjMcWilrVogSJxLWps/bF0PTl4cFdre1qtq1a73V3b6q6Ia5pdUWipv6rY1BN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Sc+dSQ2Yl+CNwo6SwYed7wuST/X25M2/wu8EBE7AF2lTxs5N3AjyJ5tkSvpLek+2iW1DaR/wizavkvEbMyEfG4pP9C8mS2OpIZJm8B9gMXSVoD7CEZR4BkWunl6S/6Z4Cb0vXvBv5G0qfTfbxtAv8ZZlXz7KNmVZLUHxEdeddhNt58asjMrODcIzAzKzj3CMzMCs5BYGZWcA4CM7OCcxCYmRWcg8DMrOAcBGZmBff/AdHI7fNkPdPYAAAAAElFTkSuQmCC",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "\n",
    "def function(x, w):         #数学模型\n",
    "    return x * w \n",
    "\n",
    "def cost(x, y, w):          #损失函数\n",
    "    cost = 0.0\n",
    "    for x_val, y_val in zip(x,y):\n",
    "        y_pred = function(x_val, w)\n",
    "        cost += (y_pred - y_val) ** 2\n",
    "    return cost/len(x)\n",
    "\n",
    "def gradient(x, y, w):      #梯度函数\n",
    "    grad = 0.0\n",
    "    for x_val, y_val in zip(x, y):\n",
    "        grad += x_val*(x_val*w-y_val)\n",
    "    return (2*grad)/len(x)\n",
    "\n",
    "x_data = [1.0, 2.0, 3.0]\n",
    "y_data = [2.0, 4.0, 6.0]\n",
    "alpha = 0.01\n",
    "w = 1.0\n",
    "epoch_list = []\n",
    "cost_list = []\n",
    "epoch = 0\n",
    "last_cost = cost(x_data, y_data, w)\n",
    "\n",
    "while True:\n",
    "    grad_val = gradient(x_data, y_data, w)\n",
    "    w -= alpha*grad_val\n",
    "    this_cost = cost(x_data, y_data, w)\n",
    "    # print('epoch:', epoch, 'w=', w, 'loss=', this_cost)\n",
    "    epoch_list.append(epoch)\n",
    "    cost_list.append(this_cost)\n",
    "    epoch += 1\n",
    "    if((last_cost - this_cost) < 1e-16):\n",
    "        break\n",
    "    last_cost = this_cost\n",
    "    \n",
    "print(\"w = \",\"%.1f\"%w,\"epoch = \",epoch)\n",
    "print(\"After train forward(4) = \",function(4, w))\n",
    "plt.plot(epoch_list,cost_list)\n",
    "plt.ylabel('cost')\n",
    "plt.xlabel('epoch')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "w =  2.0 b =  0.0 epoch =  6063\n",
      "After train forward(4) =  7.999999713401953\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYgAAAEGCAYAAAB/+QKOAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuNCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8QVMy6AAAACXBIWXMAAAsTAAALEwEAmpwYAAAf+klEQVR4nO3de3RdZ33m8e+jIx3rassXOXYsEdvgJrgh5CKMA5S2CUxtYMUsoIzTgTAMXV6ZkkJaWEwynZZpu9q5UQoZMjEmJCVDIAMhgGEZQriEW3Ox4jgXxzFRTBIrtmMlju8XSdZv/thbzvHxsX1ka+tIOs9nrbO8z7vffc7vTWw92rd3KyIwMzMrVlPpAszMbGxyQJiZWUkOCDMzK8kBYWZmJTkgzMyspNpKFzCSZsyYEXPnzq10GWZm48ZDDz30YkS0lVo3oQJi7ty5dHV1VboMM7NxQ9KzJ1rnQ0xmZlaSA8LMzEpyQJiZWUkOCDMzK8kBYWZmJTkgzMysJAeEmZmV5IAA/vdPnuLnv+mtdBlmZmNKpgEhaYmkTZK6JV1XYv15ku6TdFjSJ4vWtUq6U9KTkjZKujSrOm/6+dP86ikHhJlZoczupJaUA24E3g70AGslrY6IJwq67QQ+Bry7xEd8HvhhRLxPUh5ozKrWXI0YGPSDk8zMCmW5B7EI6I6IzRHRB9wBLCvsEBE7ImIt0F/YLmky8Fbgy2m/vojYlVWhtTVi4IgDwsysUJYBMQfYUvC+J20rx3ygF7hV0sOSbpbUVKqjpBWSuiR19fae3mG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Acu38kfW0Y+nc8TsdyIdCV/v36DjC1kuPwVBtmZlZStR9iMjOzE3BAmJlZSQ4IMzMryQFhZmYlOSDMzKwkB4TZGCDpD4ZmVDUbKxwQZmZWkgPCbBgkfUDJ8yDWS/piOnHfPkn/JGmdpJ9Iakv7XijpfkmPSvr20Dz+kl4j6cdKnimxTtKr049vLngWwO2j/SwCs2IOCLMySXot8G9JJoa7EDgC/DugCVgXyWRxPwc+nW5yG/CfIuIC4LGC9tuBGyPi9cCbSO6Uh2SG22tJ5vmfTzJfklnF1Fa6ALNx5HLgEmBt+st9A8nEaYPA/0v7fBW4S9IUoDUifp62fwX4Zjpn0JyI+DZARBwCSD/vwYjoSd+vJ3kOyK8yH5XZCTggzMon4CsRcf0xjdJfF/U72fw1JztsdLhg+Qj+92kV5kNMZuX7CfA+STPh6DOPzyH5d/S+tM+fAL+KiN3Ay5J+L23/IPDzSJ6B0SPp3elnTJLUOJqDMCuXf0MxK1NEPCHpv5A8uayGZIbdj5I82OV3JT0E7CY5TwHJ1Mwr0wDYDHw4bf8g8EVJf5d+xh+P4jDMyubZXM3OkKR9EdFc6TrMRpoPMZmZWUnegzAzs5K8B2FmZiU5IMzMrCQHhJmZleSAMDOzkhwQZmZW0v8H13hCDDLvHH4AAAAASUVORK5CYII=",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "\n",
    "def function(x, w, b):         #数学模型\n",
    "    return x * w + b \n",
    "\n",
    "def cost(x, y, w, b):          #损失函数\n",
    "    cost = 0.0\n",
    "    for x_val, y_val in zip(x,y):\n",
    "        y_pred = function(x_val, w, b)\n",
    "        cost += (y_pred - y_val) ** 2.0\n",
    "    return cost/len(x)\n",
    "\n",
    "def gradient_w(x, y, w,b):      #梯度函数\n",
    "    grad = 0.0\n",
    "    for x_val, y_val in zip(x, y):\n",
    "        grad += x_val*(x_val*w+b-y_val)\n",
    "    return (2*grad)/len(x)\n",
    "\n",
    "def gradient_b(x, y, w, b): #\n",
    "    grad = 0.0\n",
    "    for x_val, y_val in zip(x, y):\n",
    "        grad += x_val*w+b-y_val\n",
    "    return (2*grad)/len(x)\n",
    "\n",
    "x_data = [1.0, 2.0, 3.0]\n",
    "y_data = [2.0, 4.0, 6.0]\n",
    "\n",
    "alpha = 0.01\n",
    "w = 1.5\n",
    "b = 1.0\n",
    "\n",
    "epoch_list = []\n",
    "cost_list = []\n",
    "epoch = 0\n",
    "\n",
    "last_cost = cost(x_data, y_data, w, b)\n",
    "while True:\n",
    "    grad_val_w = gradient_w(x_data, y_data, w, b)\n",
    "    w -= alpha*grad_val_w\n",
    "    grad_val_b = gradient_b(x_data, y_data, w, b)\n",
    "    b -= alpha*grad_val_b\n",
    "    this_cost = cost(x_data, y_data, w, b)\n",
    "    # print('epoch:', epoch, 'w=', w, 'loss=', this_cost)\n",
    "    epoch_list.append(epoch)\n",
    "    cost_list.append(this_cost)\n",
    "    epoch += 1\n",
    "    if((abs(last_cost) - abs(this_cost)) < 1e-16):\n",
    "        break\n",
    "    last_cost = this_cost\n",
    "    \n",
    "print(\"w = \",\"%.1f\"%w,\"b = \",\"%.1f\"%b,\"epoch = \",epoch)\n",
    "print(\"After train forward(4) = \",function(4, w, b))\n",
    "plt.plot(epoch_list,cost_list)\n",
    "plt.ylabel('cost')\n",
    "plt.xlabel('epoch')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 随机梯度下降"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "predict (before training) 4 4.0\n",
      "predict (after training) 4 7.9999999999996945\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXgAAAEGCAYAAABvtY4XAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliI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Pssf9A+BXI2Jl9Wf9MnrXmUoe86DjjfNzwJURMRoRFwAvAW5f9Kdm5rL+At4AfAf4PvD+YfenxnH+G3r/Nbsb2F59vQE4m95V9+9Wv5417L7WNP5XATdVj4sfM7AZmKp+v/8eOLP0cQN/DnwbuAf4v8BoiWMGPkHvOsMsvRn6u55tnMD7q3x7AHj982nLrQokqVDLvUQjSToOA16SCmXAS1KhDHhJKpQBL0mFMuClUyAiXtXf7VI6XRjwklQoA16NEhFXRcTtEbE9Ij5S7TX/VET8r4i4MyJujoiJ6r2bI+IbEXF3RNzY36M7In42Iv4xIu6qfubF1cefMbCH+/XVHZnS0BjwaoyIeBlwBXBpZm4G5oD/CKwC7szMi4FbgP9a/cjfAv8lM38J+NbA69cDf5WZF9LbL2Vn9fpFwHvonU3wInp76UhD0x52B6QldBnwCuCOanI9Rm9Tp3ngU9V7Pg7cEBFrgLWZeUv1+nXA30XEamB9Zt4IkJkHAKrPuz0zd1TPtwObgFtrH5V0HAa8miSA6zLzmiNejPizo973bPt3PFvZZWbg8Rz+/dKQWaJRk9wMvCUifgYWzsF8Ib2/B2+p3vMfgFszcw/wk4j4ter1dwC3ZG8P/h0R8abqM0YjYuVSDkJaLGcYaozMvC8i/hT4ckS06O3m9256B2r8QkRsA/bQq9NDb9vW/1MF+IPA71avvwP4SET8RfUZb13CYUiL5m6SaryIeCozzxh2P6RTzRKNJBXKGbwkFcoZvCQVyoCXpEIZ8JJUKANekgplwEtSof4/QRxwCjhfgOcAAAAASUVORK5CYII=",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt\n",
    " \n",
    "x_data = [1.0, 2.0, 3.0]\n",
    "y_data = [2.0, 4.0, 6.0]\n",
    " \n",
    "w = 1.0\n",
    " \n",
    "def forward(x):\n",
    "    return x*w\n",
    " \n",
    "# calculate loss function\n",
    "def loss(x, y):\n",
    "    y_pred = forward(x)\n",
    "    return (y_pred - y)**2\n",
    " \n",
    "# define the gradient function  sgd\n",
    "def gradient(x, y):\n",
    "    return 2*x*(x*w - y)\n",
    " \n",
    "epoch_list = []\n",
    "loss_list = []\n",
    "print('predict (before training)', 4, forward(4))\n",
    "for epoch in range(100):\n",
    "    for x,y in zip(x_data, y_data):\n",
    "        grad = gradient(x,y)\n",
    "        w = w - 0.01*grad    # update weight by every grad of sample of training set\n",
    "        # print(\"\\tgrad:\", x, y,grad)\n",
    "        l = loss(x,y)\n",
    "    # print(\"progress:\",epoch,\"w=\",w,\"loss=\",l)\n",
    "    epoch_list.append(epoch)\n",
    "    loss_list.append(l)\n",
    " \n",
    "print('predict (after training)', 4, forward(4))\n",
    "plt.plot(epoch_list,loss_list)\n",
    "plt.ylabel('loss')\n",
    "plt.xlabel('epoch')\n",
    "plt.show() "
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "44143c5ac5f3ceb8e37c69c3af73325ae55d21292b2c7b54871fd886482dde4c"
  },
  "kernelspec": {
   "display_name": "Python 3.6.13 ('pytorch')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.13"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
